package com.yqz.sys.service.impl;

import com.yqz.common.CodeMsg;
import com.yqz.common.Constant;
import com.yqz.common.Result;
import com.yqz.common.base.Form;
import com.yqz.common.base.service.impl.BaseServiceImpl;
import com.yqz.common.util.WebUtil;
import com.yqz.sys.domain.CompUser;
import com.yqz.sys.form.CompPermissionForm;
import com.yqz.sys.mapper.CompPermissionMapper;
import com.yqz.sys.mapper.CompRoleMapper;
import com.yqz.sys.mapper.CompUserRoleRefMapper;
import com.yqz.sys.service.CompPermissionService;
import com.yqz.sys.vo.CompPermissionVO;
import com.yqz.sys.vo.CompUserRoleRefVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CompPermissionServiceImpl extends BaseServiceImpl implements CompPermissionService {

    @Autowired
    private CompPermissionMapper permissionMapper;

    @Autowired
    private CompRoleMapper roleMapper;

    @Autowired
    private CompUserRoleRefMapper userRoleRefMapper;

    public CompPermissionServiceImpl(CompPermissionMapper permissionMapper) {
        super(permissionMapper);
    }

    @Override
    public Result currentMenu() {
        CompUser user = WebUtil.getCurrentCompUser();
        if(user == null){
            return new Result(CodeMsg.PERM_USER_LOGIN_INVALID_ERROR);
        }
        Integer id = user.getId();
        Integer compId = user.getCompId();
        List<CompUserRoleRefVO> roles = userRoleRefMapper.findByCompIdAndUId(compId, id);
        List<CompPermissionVO> compPermissionVOS = permissionMapper.selectRolesPermission(roles);
        List<CompPermissionVO> menus = new ArrayList();
        List<CompPermissionVO> parentMenu = new ArrayList();
        for (CompPermissionVO compPermissionVO : compPermissionVOS) {
            if(compPermissionVO.getType().equals(Constant.PERMISSION_TYPE_MENU)){
                menus.add(compPermissionVO);
                if(compPermissionVO.getpId().equals(Constant.PERMINSSION_ROOT_ID)){
                    parentMenu.add(compPermissionVO);
                }
            }
        }
        for (CompPermissionVO menu : parentMenu) {
            Integer mid = menu.getId();
            for (CompPermissionVO compPermissionVO : menus) {
                Integer pid = compPermissionVO.getpId();
                if(mid.equals(pid)){
                    List<CompPermissionVO> children = menu.getChildren();
                    if(children == null){
                        children = new ArrayList();
                        menu.setChildren(children);
                    }
                    children.add(compPermissionVO);
                }
            }
        }
        return new Result(parentMenu);
    }

    @Override
    public Result pIdsByRole(Integer rId) {
        List<Integer> pIds = permissionMapper.findPIdByRId(rId, WebUtil.getCurrentCompUser().getCompId());
        return new Result(pIds);
    }

    @Override
    public Result add(Form form){
        CompPermissionForm compPermissionForm = (CompPermissionForm) form;
        compPermissionForm.setCompId(WebUtil.getCurrentCompUser().getCompId());
        return super.add(compPermissionForm);
    }

    @Override
    public Result delete(Integer id){
        List<Integer> pIds = new ArrayList();
        pIds.add(id);
        List<Integer> cIds = permissionMapper.findAllIdByPId(pIds);
        List<Integer> btnIds = new ArrayList();
        if(!cIds.isEmpty()){
            btnIds = permissionMapper.findAllIdByPId(cIds);
            permissionMapper.batchDeleteByPId(cIds);
        }
        cIds.addAll(pIds);
        cIds.addAll(btnIds);
        permissionMapper.deleteRoleRefByPId(cIds);
        return super.delete(id);
    }

}
